package com.atguigu.dao.impl;

import com.atguigu.dao.MysqlServiceConfigDao;
import com.atguigu.pojo.MysqlServiceConfig;

import java.util.List;

public class MysqlServiceConfigDaoImpl extends BaseDao implements MysqlServiceConfigDao {
    @Override
    public int addMysqlServiceConfig(MysqlServiceConfig mysqlServiceConfig) {
        System.out.println("MysqlServiceConfigDaoImpl 程序在["+Thread.currentThread().getName()+"]线程中");
        String sql="insert into t_mysqlServiceConfig(`id`,`serverId`,`serviceId`,`port`,`password`,`innodb_buffer_pool_size`,`max_connections`,`masterNode`) values(?,?,?,?,?,?,?,?);";
            return update(sql,mysqlServiceConfig.getId(),mysqlServiceConfig.getServerId(),
                    mysqlServiceConfig.getServiceId(),mysqlServiceConfig.getPort(),
                    mysqlServiceConfig.getPassword(),mysqlServiceConfig.getInnodb_buffer_pool_size(),mysqlServiceConfig.getMax_connections(),mysqlServiceConfig.getMasterNode());
    }

    @Override
    public int deleteMysqlServiceConfigById(String id) {
        String sql="delete from t_mysqlServiceConfig where id=?";
        return update(sql,id);
    }

    @Override
    public int updateMysqlServiceConfig(MysqlServiceConfig mysqlServiceConfig) {
        String sql="update t_mysqlServiceConfig set `serverId`=?,`serviceId`=?,`port`=?,`password`=?,`innodb_buffer_pool_size`=?,`max_connections`=?,`masterNode`=? where id=?";
        return update(sql,mysqlServiceConfig.getServerId(),
                mysqlServiceConfig.getServiceId(),mysqlServiceConfig.getPort(),
                mysqlServiceConfig.getPassword(),mysqlServiceConfig.getInnodb_buffer_pool_size(),mysqlServiceConfig.getMax_connections(),
                mysqlServiceConfig.getMasterNode(),mysqlServiceConfig.getId());
    }

    @Override
    public MysqlServiceConfig queryMysqlServiceConfigById(String id) {
        String sql ="select `id`,`serverId`,`serviceId`,`port`,`password`,`innodb_buffer_pool_size`,`max_connections`,`masterNode` from t_mysqlServiceConfig where id=?";
        return queryForOne(MysqlServiceConfig.class,sql,id);
    }

    @Override
    public List<MysqlServiceConfig> queryMysqlServiceConfigs() {
        String sql ="select `id`,`serverId`,`serviceId`,`port`,`password`,`innodb_buffer_pool_size`,`max_connections`,`masterNode`  from t_mysqlServiceConfig";
        return queryForList(MysqlServiceConfig.class,sql);
    }
    @Override
    public MysqlServiceConfig queryMysqlServiceConfigByServiceId(String serviceId){
        String sql ="select `id`,`serverId`,`serviceId`,`port`,`password`,`innodb_buffer_pool_size`,`max_connections`,`masterNode`  from t_mysqlServiceConfig where serviceId =?";
        return queryForOne(MysqlServiceConfig.class,sql,serviceId);
    }
    @Override
    public List<MysqlServiceConfig> queryMysqlServiceConfigsByPort(String port) {
        String sql ="select `id`,`serverId`,`serviceId`,`port`,`password`,`innodb_buffer_pool_size`,`max_connections`,`masterNode` from t_mysqlServiceConfig where port =?";
        return queryForList(MysqlServiceConfig.class,sql,port);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql="select count(*) from t_mysqlServiceConfig";
        Number serverCount=(Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return serverCount.intValue();
    }

    @Override
    public List<MysqlServiceConfig> queryForPageItems(Integer begin, Integer pageSize) {
        String sql ="select `id`,`serverId`,`serviceId`,`port`,`password`,`innodb_buffer_pool_size`,`max_connections`,`masterNode` from t_mysqlServiceConfig limit ?,?";
        return queryForList(MysqlServiceConfig.class,sql,begin,pageSize);

    }
}
